﻿using System.IO;
using ComponentSystem;
using Cysharp.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using UnityEngine;

namespace LubanSystem
{
    /// <summary>
    /// StreamingAssets路径支持组件
    /// </summary>
    public partial class StreamingAssets_Com : ComponentWithId
    {
        /// <summary>
        /// StreamingAssets基础路径
        /// </summary>
        public string basePath => Application.streamingAssetsPath;
    }

    public partial class StreamingAssets_Com
    {
        /// <summary>
        /// 检查文件是否存在
        /// </summary>
        /// <param name="assetPath">文件路径</param>
        /// <returns>是否存在</returns>
        public bool Exist(string assetPath)
        {
            if (string.IsNullOrEmpty(assetPath))
            {
                //Debug.LogError($"LubanSystem StreamingAssets_Com assetPath 为空");
                return false;
            }

            string fullPath = Path.Combine(basePath, assetPath);
            bool isExist = File.Exists(fullPath);
            
            return isExist;
        }

        /// <summary>
        /// 读取文件
        /// </summary>
        /// <param name="path">文件路径</param>
        /// <returns>文件内容</returns>
        public string LoadConfigs(string path)
        {
            if (!this.Exist(path)) return string.Empty;

            try
            {
                string fullPath = Path.Combine(basePath, path);
                return File.ReadAllText(fullPath, System.Text.Encoding.UTF8);
            }
            catch (System.Exception e)
            {
                Debug.LogError($"LubanSystem StreamingAssets_Com 读取文件失败: {path}, 错误: {e.Message}");
                return string.Empty;
            }
        }
    }
}